package com.xiaotou.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaotou.db.dao.CommonDAO;
import com.xiaotou.db.pojo.ActingCareer;
import com.xiaotou.db.pojo.ActionTask;
import com.xiaotou.db.pojo.AppUser;
import com.xiaotou.db.pojo.ObjectFactory;
import com.xiaotou.db.pojo.WbUser;
import com.xiaotou.domain.AvailableItem;
import com.xiaotou.domain.Engine;
import com.xiaotou.domain.Prey;
import com.xiaotou.domain.Roles;
import com.xiaotou.domain.Thief;
import com.xiaotou.weibo.WeiboClient;
import com.xiaotou.weibo.WeiboClientException;
import com.xiaotou.weibo.WeiboFactory;
import com.xiaotou.weibo.WeiboUser;

@Service
public class ActionService {

	private CommonDAO commonDao;

	private Engine engine;

	@Transactional
	public ActionTask gathering(WbUser thiefWbUser, Long preyId)
			throws WeiboClientException {

		final List<WbUser> wbUsers = this.commonDao
				.findByNamedQueryAndNamedParam(WbUser.FIND_BY_WB_ID, "wbId",
						preyId);
		WbUser preyWbUser = null;
		if (wbUsers.iterator().hasNext()) {
			preyWbUser = wbUsers.iterator().next();
		} else {
			final WeiboClient weiboClient = WeiboFactory
					.createWeiboClient(thiefWbUser);
			final WeiboUser weiboUser = weiboClient.getUser(preyId.toString());
			preyWbUser = ObjectFactory.createWbUser(weiboUser);
			this.commonDao.save(preyWbUser);
		}

		final Thief thief = this.engine.actThief(WeiboFactory
				.createWeiboUser(thiefWbUser));
		final Prey prey = engine.actPrey(WeiboFactory
				.createWeiboUser(preyWbUser));

		final ActionTask action = thief.target(prey);

		this.commonDao.save(action);

		return action;
	}

	public Set<AvailableItem> getItems(Long actionId) {
		final ActionTask actionTask = this.commonDao.get(ActionTask.class,
				actionId);
		final Prey prey = this.engine.actPrey(WeiboFactory
				.createWeiboUser(actionTask.getPrey()));
		return prey.getAvailableItems();
	}

	@Transactional(readOnly = true)
	public ActionTask getActionTask(Long actionId) {
		return this.commonDao.get(ActionTask.class, actionId);
	}

	@Transactional
	public ActionTask start(Long actionId) {
		final ActionTask actionTask = this.commonDao.get(ActionTask.class,
				actionId);
		final Thief thief = this.engine.actThief(WeiboFactory
				.createWeiboUser(actionTask.getThief()));

		thief.action(actionTask);

		return actionTask;
	}

	@Transactional
	public ActionTask done(Long actionId, int gotMoney) {
		final ActionTask actionTask = this.commonDao.get(ActionTask.class,
				actionId);
		actionTask.setGotMoney(gotMoney);
		final Thief thief = this.engine.actThief(WeiboFactory
				.createWeiboUser(actionTask.getThief()));
		final AppUser appUser = actionTask.getThief().getAppUser();
		ActingCareer actingCareer = appUser.getActingCareers().get(
				Roles.Thrief.getValue());
		if (actingCareer == null) {
			actingCareer = new ActingCareer();
			actingCareer.setAppUser(appUser);
			actingCareer.setStartTime(new Date());
			this.commonDao.save(actingCareer);
			appUser.getActingCareers().put(Roles.Thrief.getValue(),
					actingCareer);
		}

		thief.done(actionTask);

		return actionTask;
	}

	@Transactional
	public ActionTask snapshot(Long actionId, String contextRealPath) {
		final ActionTask actionTask = this.commonDao.get(ActionTask.class,
				actionId);

		engine.snapshot(actionTask, contextRealPath);

		return actionTask;
	}

	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}
}